<template>
  <div class="pieFullChart">
    <div :id="id" :class="className" :style="{ height: height, width: '300px' }" />
    <div class="legend">
      <div v-for="item of seriesData" class="legend-single">
        <div class="circle" :style="{background: item.itemStyle.color}"></div>
        <div class="key">{{item.legendname}}</div>
        <div class="value">{{item.value}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";
import { debounce } from "@/utils";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "PieFullChart"
    },
    id: {
      type: String,
      default: "PieFullChart"
    },
    width: {
      type: String,
      default: "300px"
    },
    height: {
      type: String,
      default: "200px"
    },
    labelShow: {
      type: Boolean,
      default: false
    },
    tooltipShow: {
      type: Boolean,
      default: true
    },
    seriesData: {
      type: Array,
      default: function() {
        return [
          {
            value: 3,
            legendname: "项目1",
            name: "项目1 3",
            itemStyle: {
              color: "#921140"
            }
          },
          {
            value: 3,
            legendname: "项目02",
            name: "项目2 3",
            itemStyle: {
              color: "#bb0505"
            }
          },
          {
            value: 2,
            legendname: "项目03",
            name: "项目3 2",
            itemStyle: {
              color: "#fc2a2a"
            }
          },
          {
            value: 1,
            legendname: "项目4",
            name: "项目4 1",
            itemStyle: {
              color: "#fa6633"
            }
          },
          {
            value: 3,
            legendname: "项目5",
            name: "项目5 3",
            itemStyle: {
              color: "#fca223"
            }
          },
          {
            value: 3,
            legendname: "项目6",
            name: "项目6 3",
            itemStyle: {
              color: "#fcd125"
            }
          }
        ];
      }
    },
      abnormalTotal: {
        type: Number,
        default: 0
      }
  },
  data() {
    return {
      chart: null,
      // width1: '300px',
      textStyleFontSize: 20,
      subTextStyleFontSize: 12,
      top1: "35%",
      resizeHandOwn: () => {}
    };
  },
  mounted() {
    this.resizeHandOwn = debounce(() => {
      this.autoChagneChart();
    }, 300);
    setTimeout(() => {
      this.width1 = this.width;
      console.log(this.width1, 111);
      this.initChart();
    }, 600);
    window.addEventListener("resize", this.resizeHandOwn);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
    window.removeEventListener("resize", this.resizeHandOwn);
  },
  methods: {
    autoChagneChart() {
      let windowWidth = document.body.clientWidth;
      //   if (windowWidth < 1400) {
      // 	  console.log('gg')
      //     this.textStyleFontSize = 14;
      //     this.subTextStyleFontSize = 10;
      //     this.top1 = "10%";
      //     this.initChart();
      //   } if (windowWidth < 1950) {
      //     this.textStyleFontSize = 20;
      //     this.subTextStyleFontSize = 12;
      //     this.top1 = "50%";
      //     this.initChart();
      //   } else {
      //     this.textStyleFontSize = 20;
      //     this.subTextStyleFontSize = 12;
      // 	this.top1 = "50%";
      // 	console.log('bbb')
      //     this.initChart();
      //   }
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        title: [
          //   {
          //     text: "标题",
          //     textStyle: {
          //       fontSize: 16,
          //       color: "black"
          //     },
          //     left: "2%"
          //   },
          {
            text: this.abnormalTotal,
            subtext: "异常总数",
            textStyle: {
              fontSize: this.textStyleFontSize,
              color: "#fff"
            },
            subtextStyle: {
              fontSize: this.subTextStyleFontSize,
              color: "#fff"
            },
            textAlign: "center",
            x: "24%",
            y: this.top1
          }
        ],
        tooltip: {
          show: this.tooltipShow,
          trigger: "item",
          formatter: function(parms) {
            var str =
              //   parms.seriesName +
              //   "</br>" +
              parms.marker +
              "" +
              parms.data.legendname +
              "</br>" +
              "数量：" +
              parms.data.value +
              "</br>" +
              "占比：" +
              parms.percent +
              "%";
            return str;
          },
          textStyle: {
            fontSize: 10
          }
        },
        legend: {
          show: false,
          orient: "vertical",
          left: "45%",
          align: "left",
          top: "30%",
          // textStyle: {
          //   color: "#fff",
          //   fontSize: '8px'
          // },
          textStyle: {
            color: "#ffffff",
            fontSize: 12 // 14
          },
          icon: "circle",
          height: 90, // 250
          itemWidth: 10,
          itemGap: 15
        },
        series: [
          {
            name: "标题",
            type: "pie",
            center: ["25%", "50%"],
            radius: ["40%", "55%"],
            clockwise: false, //饼图的扇区是否是顺时针排布
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: this.labelShow,
                position: "outter",
                formatter: function(parms) {
                  // console.log(parms)
                  var str =
                    parms.data.legendname +
                    "  " +
                    parms.data.value +
                    " 占比：" +
                    parms.percent +
                    "%";
                  return str;
                  //   return parms.data.legendname;
                }
              }
            },
            labelLine: {
              //   show: false,
              normal: {
                length: 5,
                length2: 10,
                smooth: true
              }
            },
            data: this.seriesData
          }
        ]
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.pieFullChart {
  height: 100%;
  width: 100%;
  position: relative;
  .legend {
    width: 55%;
    position: absolute;
    right: 5px;
    top: 30%;
    font-size: 18px;
    .legend-single {
      width: 36%;
      margin: 2% 3%;
      display: inline-block;
      div {
        display: inline-block;
        font-size: 10px;
      }
      .circle {
        width: 12px;
        height: 12px;
        border-radius: 50%;
      }
      .value {
        float: right;
        // margin-top: 5px;
        font-size: 10px;
      }
    }
  }
}
</style>
